package cn.tedu.csmall.product.service.impl;

import cn.tedu.csmall.product.ex.ServiceException;
import cn.tedu.csmall.product.mapper.PictureMapper;
import cn.tedu.csmall.product.pojo.dto.PictureAddNewDTO;
import cn.tedu.csmall.product.pojo.entity.Picture;
import cn.tedu.csmall.product.pojo.vo.PictureStandardVO;
import cn.tedu.csmall.product.service.IPictureService;
import cn.tedu.csmall.product.web.ServiceCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class PictureServiceImpl implements IPictureService {
    @Autowired
    private PictureMapper mapper;

    @Override
    public void addNew(PictureAddNewDTO addNewDTO) {
        Picture picture = new Picture();
        BeanUtils.copyProperties(addNewDTO,picture);
        int insertRows = mapper.insert(picture);
        if (insertRows != 1){
            String message = "添加图片失败，服务器繁忙，请稍后重试！";
            throw new ServiceException(ServiceCode.ERR_INSERT,message);
        }
    }

    @Override
    public void deleteById(Long id) {
        PictureStandardVO standardVO = mapper.getStandardById(id);
        if (standardVO == null) {
            String message = "删除图片失败，该图片不存在！";
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,message);
        }

        int deleteRows = mapper.deleteById(id);
        if (deleteRows != 1){
            String message = "删除图片失败，服务器繁忙，请稍后重试！";
            throw new ServiceException(ServiceCode.ERR_DELETE,message);
        }
    }
}
